package com.foreknow.elm.dao.Impl;

import com.foreknow.elm.dao.OrderdetailetDao;
import com.foreknow.elm.po.Food;
import com.foreknow.elm.po.Orderdetailet;
import com.foreknow.elm.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrderdetailetDaoImpl implements OrderdetailetDao {
    private Connection con ;
    private PreparedStatement pst;
    private ResultSet rs;
    @Override
    public Integer saveOrderdetailet(List<Orderdetailet> list) throws SQLException {
        String sql = "insert into orderdetailet(orderId,foodId,quantity) value";
        StringBuffer s = new StringBuffer(sql);
        //遍历list集合，使SQL语句能够加入大量数据
        for (Orderdetailet orderdetailet:list){
            s.append("("+orderdetailet.getOrderId()+","+orderdetailet.getFoodId()+","+orderdetailet.getQuantity()+"),");
        }
        sql = s.substring(0,s.lastIndexOf(","));
        int result = 0;
        try {
            //Connection从ThreadLocal中获取
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            result = pst.executeUpdate();
        }finally {                          //这里不能处理异常，也就是没有catch，只有finally
            DBUtil.close(pst);              //这里负责关闭PreparedStatement和ResultSet
        }
        return result;
    }

    @Override
    public List<Orderdetailet> listOrderOrderdetailetByorderId(Integer orderId) throws SQLException {
        // 根据orderId查询订单详细信息和对应的详细食物信息
        String sql = "SELECT * from orderdetailet o,food f WHERE o.foodId=f.foodId AND o.orderId=?";
        List<Orderdetailet> list = new ArrayList<>();
        try {
            //Connection从ThreadLocal中获取
            Connection con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setInt(1,orderId);
            //执行查询，并将查询到的结果放到结果集
            //遍历结果集并将每行数据保存到List集合中
            rs = pst.executeQuery();
            while (rs.next()){
                Orderdetailet orderdetailet = new Orderdetailet();
                Food food = new Food();

                orderdetailet.setOdId(rs.getInt("odId"));
                orderdetailet.setOrderId(rs.getInt("orderId"));
                orderdetailet.setFoodId(rs.getInt("foodId"));
                orderdetailet.setQuantity(rs.getInt("quantity"));

                food.setFoodId(rs.getInt("foodId"));
                food.setFoodName(rs.getString("foodName"));
                food.setFoodExplain(rs.getString("foodExplain"));
                food.setFoodImg(rs.getString("foodImg"));
                food.setFoodPrice(rs.getDouble("foodPrice"));
                food.setBusinessId(rs.getInt("businessId"));
                food.setRemarks(rs.getString("remarks"));

                orderdetailet.setFood(food);
                list.add(orderdetailet);
            }
        }finally {                          //这里不能处理异常，也就是没有catch，只有finally
            DBUtil.close(pst);              //这里负责关闭PreparedStatement和ResultSet
        }
        return list;
    }


}